<?php
namespace app\backend\controller;

class StaffRegionApply extends Backend {
	
	public function index() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$where = array();
		$condition = array();
		$condition['realname'] = $request->get('realname', '');
		if(0 < mb_strlen($condition['realname'], 'utf8')){
			$where['staff_region_apply.realname'] = array('like', '%%'.$condition['realname'].'%%');
		};
		$condition['result'] = $request->get('result', '0');
		if((0 < mb_strlen($condition['result'], 'utf8')) && settype($condition['result'], 'integer')){
			$where['staff_region_apply.result'] = $condition['result'];
		};
		$view->assign('condition', $condition);
		
		$field = array(
			'`staff_region_apply`.*',
			'`staff`.`realname` AS `staff_realname`', '`staff`.`phone` AS `staff_phone`',
			'`region`.`name_full` AS `region_name`',
		);
		$apply_select = \app\common\model\StaffRegionApplyModel::field($field)
			->join('staff', '`staff`.`id` = `staff_region_apply`.`staff_id`', 'INNER')
			->join('region', '`region`.`id` = `staff_region_apply`.`region_id`', 'LEFT')
			->where($where)->order('staff_region_apply.id DESC')->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $apply_select->render());
		
		$apply_list = array();
		foreach($apply_select as $StaffApplyModel){
			$apply = $StaffApplyModel->toArray();
			$apply['result_name'] = $StaffApplyModel->getResultName();
			$apply_list[] = $apply;
		};
		$view->assign('data_list', $apply_list);
		
		$view->assign('result', \app\common\model\StaffApplyModel::$result);
		
		unset($where, $condition, $field, $apply_select, $StaffApplyModel);
		return $view->fetch();
		
	}
	
	public function dispose() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		$judge_2 = array_key_exists('result', $parameter) && is_bool($parameter['result']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$StaffRegionApplyModel = \app\common\model\StaffRegionApplyModel::get($parameter['id']);
		if(null === $StaffRegionApplyModel){
			return $this->disposeJson(1, '目标未找到');
		}else if(0 !== $StaffRegionApplyModel->result){
			return $this->disposeJson(1, '目标已经被处理');
		};
		
		$time = time();
		\app\common\model\Model::startTrans();
		try{
			// 保存申请数据
			$StaffRegionApplyModel->result = (true === $parameter['result']) ? 1 : 2;
			$result = $StaffRegionApplyModel->save();
			if(false == $result){
				throw new \Exception('保存申请数据失败');
			};
			
			// 申请通过，保存员工信息
			if(true === $parameter['result']){
				$StaffRegionModel = new \app\common\model\StaffRegionModel();
				$StaffRegionModel->region_id = $StaffRegionApplyModel->region_id;
				$StaffRegionModel->staff_id = $StaffRegionApplyModel->staff_id;
				$result = $StaffRegionModel->save();
				if(false == $result){
					throw new \Exception('保存员工信息失败');
				};
			};
			
			\app\common\model\Model::commit();
		
		}catch(\Exception $error){
			
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $error->getMessage());
			
		};
		
		// 返回结果
		return $this->disposeJson(0, '提交成功');
		
	}

}